import http from "@/api/http";

export async function getDatasetItems(dataset) {
  try {
    const result = await http
      .get(`/dataset/${dataset}/items`)
      .then(function (response) {
        return response.data;
      });
    return result;
  } catch (error) {}
}

export async function getDatasets() {
  try {
    const result = await http.get(`/dataset/`).then(function (response) {
      return response.data;
    });
    return result;
  } catch (error) {}
}

export async function getDataset(id) {
  try {
    const result = await http.get(`/dataset/${id}`).then(function (response) {
      console.log(response.data);
      return response.data;
    });
    return result;
  } catch (error) {}
}

export async function searchDatasetItems(keyword) {
  try {
    const result = await http
      .post(`/dataset/items/search`, { keyword: keyword })
      .then(function (response) {
        return response.data.items;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function searchMarketDataset(keyword) {
  try {
    const result = await http
      .post(`/dataset/search`, { keyword: keyword })
      .then(function (response) {
        return response.data.datasets;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function getSubscribes(page) {
  try {
    const result = await http
      .get(`/dataset/subscribes/${page}`)
      .then(function (response) {
        // console.log(response.data.subscribes);
        return response.data.subscribes;
      });
    return result;
  } catch (error) {}
}

export async function searchMySubscribe(keyword) {
  try {
    const result = await http
      .post(`/dataset/subscribe/search`, { keyword: keyword })
      .then(function (response) {
        return response.data.subscribes;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function cancelMySubscribe(subscribeId) {
  try {
    const result = await http
      .post(`/dataset/subscribe/cancel`, { subscribe: subscribeId })
      .then(function (response) {
        return response.data.success;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function addMySubscribe(datasetId) {
  try {
    const result = await http
      .post(`/dataset/subscribe/add`, { dataset: datasetId })
      .then(function (response) {
        return response.data;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function searchDatatags(dataset, keyword) {
  try {
    const result = await http
      .post(`/dataset/datatag/search`, { dataset: dataset, keyword: keyword })
      .then(function (response) {
        return response.data.datatags;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function itemsFilter(filters) {
  try {
    const result = await http
      .post(`/dataset/items/filter`, filters)
      .then(function (response) {
        console.log(response.data);
        return response.data.items;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function saveSubscribeConfig(filters) {
  try {
    const result = await http
      .post(`/dataset/subscribe/config`, filters)
      .then(function (response) {
        console.log(response.data);
        return response.data;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}

export async function getSubscribeByDataset(datasetId) {
  try {
    const result = await http
      .post(`/dataset/subscribe/get`, {dataset:datasetId})
      .then(function (response) {
        return response.data.subscribes;
      });
    return result;
  } catch (error) {
    console.log(error);
  }
}
